﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Model;
using SMS.Common.Silverlight;
using System.ComponentModel;
using SMS.Controls.ProductSVC;
using SMS.Entities;
using System.Collections.Generic;
using System.Windows.Data;

namespace SMS.ViewModel.Master
{
    public class ProductMasterViewModel : BaseNotify
    {
        ProductMasterModel _model;

        public ProductMasterViewModel()
        {
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);
            NewCommand = new DelegateCommand(New_Click, null);
            CancelCommand = new DelegateCommand(Cancel);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new ProductMasterModel();
                _model.OnServiceError += new Action<SMSException>(_model_OnServiceError);
                _model.OnSaveCompleted += new Action<int>(_model_OnSaveCompleted);
                _model.OnSearchCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Product>>(_model_OnSearchCompleted);
            }
        }

        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Product> obj)
        {
            UserMessage = obj.Count + " customers found";
            ProductList = new List<Product>(obj);
            PagedProductList = new PagedCollectionView(ProductList);
        }

        void _model_OnSaveCompleted(int obj)
        {
            UserMessage = "Customer saved successfully.";
            SelectedMaster.Product_ID = obj;
        }

        void _model_OnServiceError(SMSException obj)
        {
            ErrorMessage = obj.Message;
        }

        private List<Product> _ProductList;

        public List<Product> ProductList
        {
            get { return _ProductList; }
            set { _ProductList = value; OnPropertyChanged("ProductList"); }
        }

        private Category _SelectedCategory;

        public Category SelectedCategory
        {
            get { return _SelectedCategory; }
            set { _SelectedCategory = value; OnPropertyChanged("SelectedCategory"); }
        }
        
        private Product _SelectedMaster;

        public Product SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedProductList;

        public PagedCollectionView PagedProductList
        {
            get { return _PagedProductList; }
            set { _PagedProductList = value; OnPropertyChanged("PagedProductList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string  _Text;

        public string  Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }
        

        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {            
            _model.SearchProduct(SearchText);

            UserMessage = "Searching customer...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (SelectedMaster != null)
            {
                if (SelectedMaster.Product_Category == null)
                {
                    ErrorMessage = "Please select Product Category";
                    return;
                }
                _model.SaveProduct(SelectedMaster);

                UserMessage = "Saving customer...";
            }
            else
            {
                ErrorMessage = "No Customer to save";
            }
        }

        public ICommand NewCommand { get; set; }

        public void New_Click(object param)
        {
            if (ProductList == null)
                ProductList = new List<Product>();
            var k = new Product() { IsLive = true };
            ProductList.Add(k);
            PagedProductList = new PagedCollectionView(ProductList);
            PagedProductList.MoveToLastPage();
            SelectedMaster = k;
        }

        public ICommand CancelCommand { get; set; }
        public void Cancel(object param)
        {
            ViewModel_MainPage.MainPageContext.FrameControl.GoBack();
        }
    }
}
